package talkplace;

import java.awt.List;
import java.awt.TextArea;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.io.IOUtils;
import talkplace.mail;
public   class client   {
	private static mail Mail;
	private static mail Mail_1;
	private static mail Mail_2;
	private  Socket socket;
	private Socket socket_1;
	private static int place;
    private static String input;
    private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	   protected  void getList(List list)  throws IOException {
		
	   socket=new Socket("127.0.0.1",8888);
	 
	   Mail=new mail(0,0);   
	   receive(socket,list); 
	   Mail=new mail(1,place);
	   Timer timer=new Timer();
	   timer.schedule(new TimerTask() {
		   public synchronized void run() {
       try {
    	socket=new Socket("127.0.0.1",8888);
		receive(socket,list);

	}   catch (IOException e) {
		
		e.printStackTrace();
	}
		   }
	   },0, 1000);
	 	
	}
	   protected  void getshowArea(TextArea showArea) throws UnknownHostException, IOException {
		 
		   Timer timer=new Timer();
		   timer.schedule(new TimerTask() {
			   public synchronized void run() {
				   try {
					   socket_1=new Socket("127.0.0.1",8888);
					   Mail_1=new mail(3,place,new Date());
					
					   receive(socket_1,showArea);
				   }catch(IOException e) {e.printStackTrace();} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			   }
		   }, 0, 2000);
	   }
	   public static  void receive(Socket socket,TextArea showArea) throws UnsupportedEncodingException, IOException, ParseException {
		   socket.getOutputStream().write(Mail_1.reArea().getBytes("utf-8"));
		
		   socket.shutdownOutput();
			String get=IOUtils.toString(socket.getInputStream(),"utf-8");
			//System.out.println(get);
			showArea.append(get);

		    socket.shutdownInput();
		    socket.close();
	   }
	   public static  void receive(Socket socket,List list) throws IOException {					  	  
		   socket.getOutputStream().write(Mail.toString().getBytes("utf-8"));
		   socket.shutdownOutput();			
			InputStream is=socket.getInputStream();
			String get=""+IOUtils.toString(socket.getInputStream(),"utf-8");
			String []st=get.split(" ");
			//System.out.println(get);
			if(Mail.getRequest()==0)
			place=Integer.valueOf(st[0]);
		    list.removeAll();
		    for(int i=1;i<st.length;i++)
		    list.add("place:"+Integer.valueOf(st[i]));
		    socket.shutdownInput();
		    socket.close();
	   }
	protected synchronized void putitem(TextArea showArea) throws UnknownHostException, IOException {
		socket=new Socket("127.0.0.1",8888);
		Mail_2=new mail(2,place,new Date(),showArea.getText());
		showArea.setText("");
	
		   socket.getOutputStream().write(Mail_2.textinput().getBytes("utf-8"));
		   socket.shutdownOutput();			
		
	}
}
